package com.ioftv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ioftv.mapper.SupplierRegistrationMapper;
import com.ioftv.entity.SupplierRegistration;
import com.ioftv.service.SupplierRegistrationService;
import com.ioftv.service.base.BaseService;
import com.ioftv.dto.PageQuery;
import com.ioftv.dto.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SupplierRegistrationServiceImpl extends BaseService<SupplierRegistrationMapper, SupplierRegistration> implements SupplierRegistrationService {
    
    @Override
    public SupplierRegistration saveSupplierRegistration(SupplierRegistration supplierRegistration) {
        // 设置用户ID
        if (supplierRegistration.getUserId() == null) {
            supplierRegistration.setUserId(getCurrentUserId());
        }
        this.save(supplierRegistration);
        return supplierRegistration;
    }
    
    @Override
    public SupplierRegistration getSupplierRegistrationById(Long id) {
        LambdaQueryWrapper<SupplierRegistration> queryWrapper = createUserQueryWrapper();
        queryWrapper.eq(SupplierRegistration::getId, id);
        return this.getOne(queryWrapper);
    }
    
    @Override
    public SupplierRegistration updateSupplierRegistration(SupplierRegistration supplierRegistration) {
        // 设置用户ID
        if (supplierRegistration.getUserId() == null) {
            supplierRegistration.setUserId(getCurrentUserId());
        }
        this.updateById(supplierRegistration);
        return supplierRegistration;
    }
    
    @Override
    public boolean deleteSupplierRegistrationById(Long id) {
        LambdaQueryWrapper<SupplierRegistration> queryWrapper = createUserQueryWrapper();
        queryWrapper.eq(SupplierRegistration::getId, id);
        return this.remove(queryWrapper);
    }
    
    @Override
    public PageResult<SupplierRegistration> getSupplierRegistrationList(PageQuery pageQuery, String supplierName, String project, String expertiseType, String approvalStatus) {
        // 创建分页对象
        IPage<SupplierRegistration> page = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        
        // 构建查询条件
        LambdaQueryWrapper<SupplierRegistration> queryWrapper = createUserQueryWrapper();
        
        // 供应商名称模糊查询
        if (StringUtils.hasText(supplierName)) {
            queryWrapper.like(SupplierRegistration::getSupplierName, supplierName);
        }
        
        // 项目名称模糊查询
        if (StringUtils.hasText(project)) {
            queryWrapper.like(SupplierRegistration::getProject, project);
        }
        
        // 擅长标注类型精确查询
        if (StringUtils.hasText(expertiseType)) {
            queryWrapper.eq(SupplierRegistration::getExpertiseType, expertiseType);
        }
        
        // 审批状态查询
        if (StringUtils.hasText(approvalStatus)) {
            queryWrapper.eq(SupplierRegistration::getApprovalStatus, approvalStatus);
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(SupplierRegistration::getCreateTime);
        
        // 执行分页查询
        IPage<SupplierRegistration> result = this.page(page, queryWrapper);
        
        // 封装分页结果
        return new PageResult<>(
            result.getRecords(),
            result.getTotal(),
            pageQuery.getCurrentPage(),
            pageQuery.getPageSize()
        );
    }

    @Override
    protected void addUserCondition(LambdaQueryWrapper<SupplierRegistration> queryWrapper) {
        queryWrapper.eq(SupplierRegistration::getUserId, getCurrentUserId());
    }

    @Override
    protected void setEntityUserId(SupplierRegistration entity) {
        if (entity.getUserId() == null) {
            entity.setUserId(getCurrentUserId());
        }
    }
}